package com.situ.stmall.mapper;

import com.situ.stmall.entity.Goods;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface GoodsMapper {
    @Delete({
        "delete from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into goods (goods_sort_id, ",
        "name, dscp, price, ",
        "version, color, ",
        "suit, count)",
        "values (#{goodsSortId,jdbcType=INTEGER}, ",
        "#{name,jdbcType=VARCHAR}, #{dscp,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, ",
        "#{version,jdbcType=VARCHAR}, #{color,jdbcType=VARCHAR}, ",
        "#{suit,jdbcType=VARCHAR}, #{count,jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Goods record);

    @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column = "id", property = "pictures",
			many = @Many(select = "com.situ.stmall.mapper.GoodsPicturreMapper.selectByGoodsId"))
    })
    Goods selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, goods_sort_id, name, dscp, price, version, color, suit, count, status",
        "from goods"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<Goods> selectAll();

    @Update({
    	"<script>",
        "update goods",
        "<set>",
        	"<if test='goodsSortId!=null and goodsSortId!=0'>goods_sort_id = #{goodsSortId},</if>",
	        "<if test='name!=null'>name = #{name},</if>",
	        "<if test='dscp!=null'>dscp = #{dscp},</if>",
	        "<if test='price!=null'>price = #{price},</if>",
	        "<if test='version!=null'>version = #{version},</if>",
	        "<if test='color!=null'>color = #{color,jdbcType=VARCHAR},</if>",
	        "<if test='suit!=null'>suit = #{suit,jdbcType=VARCHAR},</if>",
	        "<if test='count!=null'>count = #{count,jdbcType=INTEGER},</if>",
	        "<if test='status!=null'>status = #{status,jdbcType=INTEGER}</if>",	          
          "</set>",
        "where id = #{id,jdbcType=INTEGER}",
        "</script>"
    })
    int updateByPrimaryKey(Goods record);

    @Select({
    		"<script>",
    		"select goods.* from goods",
    		"inner join goods_sort on goods.goods_sort_id = goods_sort.id",
    		"<where>",
    		"<if test='goods.goodsSortId!=null and goods.goodsSortId!=0'>goods_sort_id = #{goods.goodsSortId}</if>",
    		"<if test='goods.name!=null'>and goods.name like '%${goods.name}%'</if>",
    		"<if test='goods.dscp!=null'>and dscp like '%${goods.dscp}%'</if>",
    		"<if test='goods.status!=null'>and goods.status=#{goods.status}</if>",
    		"<if test='firstSortId != null and firstSortId != 0'>and parent=#{firstSortId}</if>",
    		"</where>",
    		"</script>"
    })
    @Results({
    	@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_sort_id", property="goodsSortId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="dscp", property="dscp", jdbcType=JdbcType.VARCHAR),
        @Result(column="price", property="price", jdbcType=JdbcType.DECIMAL),
        @Result(column="version", property="version", jdbcType=JdbcType.VARCHAR),
        @Result(column="color", property="color", jdbcType=JdbcType.VARCHAR),
        @Result(column="suit", property="suit", jdbcType=JdbcType.VARCHAR),
        @Result(column="count", property="count", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column = "goods_sort_id", property = "goodsSort", 
        		one = @One(select = "com.situ.stmall.mapper.GoodsSortMapper.selectByPrimaryKey")),
        @Result(column = "id", property = "pictures",
        			many = @Many(select = "com.situ.stmall.mapper.GoodsPicturreMapper.selectByGoodsId"))
    })
	List<Goods> select(@Param("goods") Goods goods,@Param("firstSortId") Integer firstSortId);
    
}